import { fixMultiple } from "./autofixpage";
import CustomHeader from '../components/CustomHeader/CustomHeader.vue';

const defaultFieldWidth = 25;


const setAggridMinwidths = (columnDefs: Array<any>, gridOption) => {
    // console.log(gridOption)

    let newArr = [];
    if (columnDefs?.length > 0) {
        // 复制列定义数组，以免直接修改原始数组
        newArr = [...columnDefs];
        let maxNumberIndex = []

        if (gridOption && gridOption.forEachNodeAfterFilter) {
            gridOption.forEachNodeAfterFilter((row: any) => {
                maxNumberIndex.push(row)
            })
        }
        // 在数组开头插入行号列定义
        newArr.unshift({
            headerName: maxNumberIndex.length || 0,
            field: 'ROW_INDECX',
            suppressSizeToFit: true,
            resizable: false,
            lockPosition: true,
            suppressMenu: true,
            sortable: false,
            pinned: 'left',
            width: 50,
            cellStyle: {
                background: '#ccc'
            },
            headerComponent: CustomHeader,
            headerCellClass: (params) => params.colDef.field === 'ROW_INDECX' ? 'row-index-header' : 'other-header',
            valueGetter: (params) => {
                const rowIndex = params.node.rowIndex
                const ROW_INDECX = rowIndex
                if (params.node.rowPinned !== 'bottom') {
                    return params.node ? ROW_INDECX + 1 : null;
                }
            }
        });

        // 对其他列进行修改或添加额外的逻辑
        newArr = newArr.map((item) => {
            if (item.checkboxSelection) {
                // 修改复选框列的属性
                item.minWidth = defaultFieldWidth;
                item.headerName = '';
                item.field = 'defaultCkBox';
                item.lockPosition = true;
                item.width = 50;
                item.cellStyle = {
                    display: 'flex',
                    aligItems: 'center',
                    justifyContent: 'center',
                };
            }
            if (item.headerName) {
                // 修改其他列的属性
                item.minWidth = defaultFieldWidth + (14);
                item['wrapHeaderText'] = true;
                item['autoHeaderHeight'] = true;
            }
            return item;
        });
    }

    return newArr;
}
export default setAggridMinwidths